/**
 * @title 格式化时间
 * @param { Date } [time] 默认时间: new Date()
 * @param { String } [format] 默认格式: {y}-{m}-{d} {h}:{i}:{s}
 * @returns { String } 2023-05-11 22:00:00
 */
export function formatTime(date = new Date(), format = "{y}-{m}-{d} {h}:{i}:{s}") {
  if (arguments.length === 0) {
    return null;
  }
  const formatObj: any = {
    'y': date.getFullYear(),
    'm': date.getMonth() + 1,
    'd': date.getDate(),
    'h': date.getHours(),
    'i': date.getMinutes(),
    's': date.getSeconds(),
    'a': date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
